﻿#include <stdio.h>
#include<stdlib.h>

typedef int Elem;
typedef struct QueueNode {
	Elem data;
	struct QueueNode* next;
	int len;
}QueueNode;
typedef struct {
	QueueNode *front, *rear;
}LinkQueue;

/*初始化*/
void InitQueue(LinkQueue& L) {
	L.front = L.rear = (QueueNode*)malloc(sizeof(QueueNode));
	L.front->next = NULL;
	L.front->len = 0;
}
/*判空*/
bool QueueEmpty(LinkQueue L) {
	if (L.front == L.rear) {
		return true;
	}
	return false;
}
/*入队*/
bool EnQueue(LinkQueue& L,Elem x) {
	QueueNode* Q = (QueueNode*)malloc(sizeof(QueueNode));
	Q->next = NULL;
	Q->data = x;
	L.rear->next = Q;
	L.rear = Q;
	L.front->len++;
	return true;
}
/*出队*/
bool DeQueue(LinkQueue& L,Elem& x) {
	if (QueueEmpty(L)) {
		return false;
	}
	QueueNode* a = L.front->next;
	x = a->data;
	L.front->next = a->next;
	if (L.rear == a) {
		L.rear = L.front;
	}
	free(a);
	L.front->len--;
	return true;
}
/*获取头指针元素*/
bool GetHead(LinkQueue L,Elem& x) {
	if (QueueEmpty(L)) {
		printf("队列空\n");
		return false;
	}
	x = L.front->next->data;
	return true;
}
int main() {
	LinkQueue a;
	InitQueue(a);
	EnQueue(a, 3);
	EnQueue(a, 4);
	EnQueue(a, 5);
	Elem flag;
	GetHead(a, flag);
	printf("%3d\n", flag);// 3
	DeQueue(a, flag);
	printf("%3d\n", flag);// 3
	DeQueue(a, flag);
	printf("%3d\n", flag);// 4
	DeQueue(a, flag);
	printf("%3d\n", flag);// 5
	GetHead(a, flag);// 队列空
	return 0;
}